<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tiaojian:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:tiaojian:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tiaojian:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tiaojian:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tiaojianList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="是否推荐" align="center" prop="appointTeamType" >
        <template slot-scope="scope">
          <span>{{scope.row.appointTeamType=='4'?'是':'全部'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="货物品种" align="center" prop="parentCatalogId" :formatter="format1" />
      <el-table-column label="业务类型" align="center" prop="carrierCarryMode" :formatter="format2"  />
      <el-table-column label="取单地" align="center" prop="billPlate" />
      <el-table-column label="装货地" align="center" prop="startPlate" />
      <el-table-column label="目的地" align="center" prop="endPlate" />
      <el-table-column label="委托人" align="center" prop="clienter" />
      <el-table-column label="是否预付" align="center" prop="prepayFlag" >
        <template slot-scope="scope">
          <span>{{scope.row.appointTeamType=='1'?'是':'否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:tiaojian:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tiaojian:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改抢单条件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否推荐" prop="parentCatalogId">
          <el-select v-model="form.appointTeamType"  placeholder="请选择">
            <el-option label="全部" value="" />
            <el-option label="推荐" value="4" />
          </el-select>

        </el-form-item>

        <el-form-item label="货物品种" prop="parentCatalogId">

          <el-select v-model="form.parentCatalogId"  placeholder="请选择">
            <el-option label="全部" value="" />
            <el-option label="钢铁" value="1" />
            <el-option label="矿石" value="2" />
            <el-option label="焦炭" value="3" />
            <el-option label="煤炭" value="4" />
            <el-option label="化肥" value="5" />
          </el-select>

        </el-form-item>
        <el-form-item label="业务类型" prop="carrierCarryMode">
          <el-select v-model="form.carrierCarryMode"  placeholder="请选择">
            <el-option label="全部" value="" />
            <el-option label="计划模式" value="1" />
            <el-option label="单车模式" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="排除取单地"   prop="billPlate">
          <el-input v-model="form.billPlate" type="text"  placeholder="请输入取单地,多个已;隔开" />
        </el-form-item>
        <el-form-item label="排除装货地" prop="startPlate">
          <el-input v-model="form.startPlate" type="text" placeholder="请输入取单地,多个已;隔开" />
        </el-form-item>
        <el-form-item label="排除目的地" prop="endPlate">
          <el-input v-model="form.endPlate" type="text" placeholder="请输入取单地,多个已;隔开" />
        </el-form-item>
        <el-form-item label="委托人" prop="clienter">
          <el-input v-model="form.clienter" placeholder="请输入委托人" />
        </el-form-item>
        <el-form-item label="是否预付" prop="prepayFlag">
          <el-select v-model="form.prepayFlag"  placeholder="请选择">
            <el-option label="全部" value="" />
            <el-option label="是" value="0" />
            <el-option label="否" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTiaojian, getTiaojian, delTiaojian, addTiaojian, updateTiaojian, exportTiaojian } from "@/api/system/tiaojian";

export default {
  name: "Tiaojian",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抢单条件表格数据
      tiaojianList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询抢单条件列表 */
    getList() {
      this.loading = true;
      listTiaojian(this.queryParams).then(response => {
        this.tiaojianList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        parentCatalogId: null,
        carrierCarryMode: null,
        billPlate: null,
        startPlate: null,
        endPlate: null,
        clienter: null,
        prepayFlag: null,
        remarks: null,
        appointTeamType:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加抢单条件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getTiaojian(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改抢单条件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateTiaojian(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTiaojian(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$confirm('是否确认删除抢单条件编号为"' + userIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTiaojian(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有抢单条件数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTiaojian(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    format1(row, column) {
        if (row.parentCatalogId == "1") {
            return "钢铁";
        }else if (row.parentCatalogId == "2") {
            return "矿石";
        }else if (row.parentCatalogId == "3") {
            return "焦炭";
        }else if (row.parentCatalogId == "4") {
            return "煤炭";
        }else if (row.parentCatalogId == "5") {
            return "化肥";
        }else{
            return '全部';
        }
    },
    format2(row, column) {
          if (row.carrierCarryMode == "1") {
              return "计划模式";
          }else if (row.carrierCarryMode == "2") {
              return "单车模式";
          }else{
              return '全部';
          }
      }
  }
};
</script>
